package com.bluestar.tools.module.camera;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;

import com.bluestar.tools.R;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.disposables.Disposable;

public class CameraActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = CameraActivity.class.getSimpleName();

    private CameraSurfaceView mCameraSurfaceView;
    private FrameLayout mSurfaceContainer;
    private Button mSwitchBt;
    private Disposable disposable;
    private String[] requestPermissions = new String[]{
        Manifest.permission.CAMERA
    };

    private int mOrientation;
    private boolean mCameraRequested;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_camera);

        // Android 6.0相机动态权限检查
        RxPermissions rxPermissions = new RxPermissions(CameraActivity.this);
        disposable = rxPermissions
            .request(requestPermissions)
            .subscribe(granted -> {
                if (granted) { // Always true pre-M
                    // I can control the camera now
                    Log.i("permissions", Manifest.permission.CAMERA + "：获取成功");
                    mCameraRequested = true;
                    initView();
                } else {
                    // Oups permission denied
                    Log.i("permissions", Manifest.permission.CAMERA + "：获取失败");
                }
            });
        //initView();
    }

    /**
     * 初始化View
     */
    private void initView() {
        Log.d(TAG, "initView: ");
        mSurfaceContainer = findViewById(R.id.activity_camera_surface_view_container);
        mCameraSurfaceView = new CameraSurfaceView(this);
        mSurfaceContainer.addView(mCameraSurfaceView);
        mOrientation = CameraUtils.calculateCameraPreviewOrientation(CameraActivity.this);
        mSwitchBt = findViewById(R.id.switch_bt);
        mSwitchBt.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mCameraRequested) {
            Log.d(TAG, "onResume: ");
            CameraUtils.startPreview();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        CameraUtils.stopPreview();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    // 创建 item
    private ArrayList<String> mCameraIdList = new ArrayList<>();
    private String[] cameraIdItems = new String[]{};


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.switch_bt:
                //switchCamera();
                mCameraIdList.clear();
                for (int i = 0; i < CameraProvide.getCameraNumber(); i++) {
                    mCameraIdList.add("camera id : " + i);
                }

                AlertDialog alertDialog3 = new AlertDialog.Builder(this)
                    .setTitle("请选择 Camera ID")
                    .setIcon(R.mipmap.ic_launcher)
                    .setItems(mCameraIdList.toArray(cameraIdItems), new DialogInterface.OnClickListener() {// 添加列表
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Log.d(TAG, "onClick: i = " + i);
                            switchCamera(i);
                        }
                    })
                    .create();
                alertDialog3.show();
                break;
        }
    }

    /**
     * 切换相机
     */
    private void switchCamera() {
        if (mCameraSurfaceView != null) {
            CameraUtils.switchCamera(1 - CameraUtils.getCameraID(), mCameraSurfaceView.getHolder());
            // 切换相机后需要重新计算旋转角度
            mOrientation = CameraUtils.calculateCameraPreviewOrientation(CameraActivity.this);
        }
    }

    /**
     * 切换相机
     */
    private void switchCamera(int cameraId) {
        if (mCameraSurfaceView != null) {
            CameraUtils.switchCamera(cameraId, mCameraSurfaceView.getHolder());
            // 切换相机后需要重新计算旋转角度
            mOrientation = CameraUtils.calculateCameraPreviewOrientation(CameraActivity.this);
        }
    }
}